package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.dialog.BottomSlide;
import com.blockchain.securewallet.model.db.BlockAddress;
import com.blockchain.securewallet.utils.Logger;
import com.blockchain.securewallet.utils.StringUtil;
import com.blockchain.securewallet.utils.ToastUtils;
import com.blockchain.securewallet.utils.ToolUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressAddActivity extends BaseActivity {

    private static final String TAG = "Wallet.AddAddress";

    public static final String KEY_ADDRESS = "key_address";

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.edit_class)
    TextView tvClass;

    @BindView(R.id.edit_address)
    EditText editAddress;

    @BindView(R.id.edit_name)
    EditText editNote;

    // 默认为比特币类型
    // 0 BTC
    // 1 ETH
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_add);
        ButterKnife.bind(this);
        tvTitle.setText(R.string.add_address);
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    @OnClick(R.id.edit_class)
    void getWalletType() {
        BottomSlide dialog = BottomSlide.newInstance("", new String[]{"BTC", "ETH"});
        dialog.show(getSupportFragmentManager(), "dialog");
        //add item click listener
        dialog.setListener(new BottomSlide.OnClickListener() {
            @Override
            public void click(int position) {
                if (position == 0) {
                    type = BlockAddress.TYPE_BIT_CON;
                    tvClass.setText("BTC");
                } else {
                    type = BlockAddress.TYPE_ETH_COIN;
                    tvClass.setText("ETH");
                }
            }
        });
    }

    @OnClick(R.id.btn_import_address)
    void addAddress() {
        String location = editAddress.getText().toString();
        if (StringUtil.isEmpty(location)) {
            ToastUtils.showToast("地址不能为空");
            return;
        }

        //不是正确的地址
        if (!ToolUtils.isCorrectAddress(location.trim())) {
            //地址格式不正确
            ToastUtils.showToast("地址格式不正确");
            return;
        }
        
        String note = editNote.getText().toString();
        if (StringUtil.isEmpty(note)) {
            ToastUtils.showToast("名称备注不能为空");
            return;
        }

        if (note.trim().length() > 10) {
            //名称备注不能超过10位
            ToastUtils.showToast("名称备注不能超过10位");
            return;
        }

        BlockAddress address = new BlockAddress();
        address.setType(type);
        address.setNote(note);
        address.setAddress(location);
        Log.i(TAG, "address:" + address);
        
        try {
            WalletApp.getInstance().getDaoSession().getBlockAddressDao().insert(address);
        } catch (Exception e) {
            //UNIQUE constraint failed: BLOCK_ADDRESS.ADDRESS (code 2067)
            //BlockAddress{id=null, type=0, address='', note=''}    //Unique 唯一约束
            //address作为unique列，其他字段为空它就不能为空，它为空是为了与其他字段作区别
            Logger.ds("插入新地址错误:" + e.getMessage()); 
            return;
        }
        finish();
    }

    @OnClick(R.id.iv_qr_scan)
    void scan() {
        Intent intent = new Intent();
        intent.putExtra(QrCodeScanActivity.KEY_MODULE_FROM, QrCodeScanActivity.MODULE_ADD_ADDRESS);
        intent.putExtra(SendCoinActivity.KEY_TYPE_COIN, type);
        intent.setClass(this, QrCodeScanActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String qrContent = data.getStringExtra(KEY_ADDRESS);
            String address;
            try {
                JSONObject jsonObject = new JSONObject(qrContent);
                address = jsonObject.getString("a");
            } catch (Exception e) {
                address = qrContent;
            }
            editAddress.setText(address);
        }
    }
}
